Ext.define("App.datacollection.dctask.view.CollectionTaskEditor", {
    extend: 'Extend.window.FormWindow',
    alias: 'widget.CollectionTaskEditor',
    entity: 'entity',
    requires: ['App.datacollection.dctask.view.field.TaskParameterGridField', 'App.project.field.ProjectComboBox',
        'App.datacollection.dctask.view.CollectionTaskSelection',
        'App.commons.field.MonthDaysField',
        'App.commons.field.WeekDaysField'],
    config: {
        window: {
            title: '数据采集接口编辑',
            width: 900,
            height: 500,
            resizable: false
        }
    },
    defaults: {
        columnWidth: 1
    },
    items: [{
        xtype: 'hidden',
        name: 'id'
    }, {
        xtype: 'hidden',
        name: 'projectId'
    }, {
        xtype: 'textfield',
        fieldLabel: '任务名称',
        name: 'name',
        allowBlank: false,
        blankText: '接口名称为必填字段，不能为空！',
        maxLength: 256,
        columnWidth: .5,
        beforeLabelTextTpl: ['<span style="color:red;">*</span>']
    }, {
        fieldLabel: '接口',
        xtype: 'combobox',
        name: 'collectionInterface.id',
        editable: false,
        multiSelect: false,
        displayField: 'name',
        valueField: 'id',
        allowBlank: false,
        blankText: '接口为必填字段，不能为空！',
        beforeLabelTextTpl: ['<span style="color:red;">*</span>'],
        columnWidth: .5,
        store: {
            fields: ['id', 'name'],
            pageSize: 999,
            proxy: {
                type: 'ajax',
                url: 'datacollection/interface/query',
                reader: {
                    type: 'json',
                    rootProperty: 'data'
                }
            },
            autoLoad: true
        },
        listeners: {
            select: function ($this, ai) {
                var view = this.up();
                if (!Ext.isEmpty(ai)) {
                    view.resetParameters(ai.get('parameters'));
                }
            }
        }
    }, {
        xtype: 'textfield',
        fieldLabel: '接口描述',
        name: 'description'
    }, {
        xtype: 'tabpanel',
        items: [{
            xtype: 'ScheduleSetting',
            title: '调试配置'
        }, {
            title: '接口参数',
            xtype: 'TaskParameterGridField',
            name: 'parameters',
            height: 300,
            submitValue: false,
            columnWidth: 1
        }]
    }],
    getParams: function () {
        var datas = this.down('TaskParameterGridField').getDatas();
        var parameters = Ext.encode(datas);
        var params = {};
        Ext.Array.each(datas, function (data) {
            params['entity.parameters'] = parameters;
        });
        return params;
    },
    loadRecord: function (record) {
        this.record = record;
        this.initParameters(record);
        this.callParent(arguments);
    },
    resetParameters: function (ips) {

        this.down('TaskParameterGridField').getStore().removeAll();
        var ips = ips;
        if (Ext.isString(ips)) {
            if (Ext.isEmpty(ips)) {
                ips = [];
            } else {
                ips = Ext.decode(ips);
            }
        }

        this.down('TaskParameterGridField').loadDatas(ips)
    },
    initParameters: function (record) {
        var parameters = record.get('parameters');
        if (Ext.isString(parameters)) {
            if (Ext.isEmpty(parameters)) {
                parameters = [];
            } else {
                parameters = Ext.decode(parameters);
            }
        }
        if (!Ext.isEmpty(record.get('collectionInterface'))) {
            var ips = record.get('collectionInterface').parameters;
            if (Ext.isString(ips)) {
                if (Ext.isEmpty(ips)) {
                    ips = [];
                } else {
                    ips = Ext.decode(ips);
                }
            }
            // parameters = parameters.concat(ips)

            for (var i = 0; i < ips.length; i++) {
                var p = ips[i];
                var currentParameter = Ext.Array.findBy(parameters, function (item) {
                    if (item.name == p.name) {
                        return true;
                    }
                    return false;
                })
                if (Ext.isEmpty(currentParameter)) {
                    parameters = Ext.Array.insert(parameters, i, [p]);
                }
            }
            for (var i = parameters.length - 1; i >= 0; i--) {
                var currentParameter = parameters[i];
                var ip = Ext.Array.findBy(ips, function (item) {
                    if (item.name == currentParameter.name) {
                        return true;
                    }
                    return false;
                })
                if (Ext.isEmpty(ip)) {
                    parameters = Ext.Array.remove(parameters, currentParameter);
                }
            }
        }
        record.set('parameters', parameters)
    }
});


Ext.define("App.datacollection.dctask.view.ScheduleSetting", {
    extend: 'Ext.panel.Panel',
    alias: ['widget.ScheduleSetting'],
    layout: 'column',
    requires: ['App.commons.field.DateTime'],
    defaults: {
        labelAlign: 'right',
        labelWidth: 80,
        margin: 5,
        columnWidth: .51
    },
    items: [{
        fieldLabel: '重复模式',
        xtype: 'combobox',
        name: 'repeatMode',
        editable: false,
        allowBlank: false,
        blankText: '重复模式为必填字段，不能为空！',
        beforeLabelTextTpl: ['<span style="color:red;">*</span>'],
        store: ['周期性重复', '每月', '每周', '高级'],
        listeners: {
            change: function (combo, nv, ov) {
                var cardBox = this.up('CollectionTaskEditor').down('[itemId="card"]');
                cardBox.getLayout().getActiveItem().disable();
                var target = this.up('CollectionTaskEditor').down('')
                if (nv == '周期性重复') {
                    var target = this.up('CollectionTaskEditor').down('[itemId="periodic"]');
                    cardBox.getLayout().setActiveItem(target);
                    target.enable();
                } else if (nv == '高级') {
                    var target = this.up('CollectionTaskEditor').down('[itemId="custom"]');
                    cardBox.getLayout().setActiveItem(target);
                    target.enable();
                } else if (nv == '每周') {
                    var target = this.up('CollectionTaskEditor').down('[itemId="week"]');
                    cardBox.getLayout().setActiveItem(target);
                    target.enable();
                } else if (nv == '每月') {
                    var target = this.up('CollectionTaskEditor').down('[itemId="month"]');
                    cardBox.getLayout().setActiveItem(target);
                    target.enable();
                }
            }
        }
    }, {
        fieldLabel: '是否挂起',
        xtype: 'BooleanComboBox',
        name: 'disabled',
        allowBlank: false,
        blankText: '是否禁用为必填字段，不能为空！',
        beforeLabelTextTpl: ['<span style="color:red;">*</span>']
    }, {
        fieldLabel: '开始时间',
        xtype: 'datetimefield',
        name: 'triggerStartDatetime',
        emptyText: '为空时立即开始',
        //editable: false,
        format: 'Y-m-d h:i'
    }, {
        xtype: 'fieldset',
        title: '计划配置',
        layout: 'card',
        itemId: 'card',
        columnWidth: 1,
        padding: '5 0 5 0',
        defaults: {
            layout: 'column',
            xtype: 'fieldcontainer',
            disabled: true,
            defaults: {
                labelAlign: 'right',
                labelWidth: 100,
                margin: 5
            }
        },
        items: [{
            itemId: 'week',
            items: [{
                fieldLabel: '执行时间',
                xtype: 'timefield',
                name: 'executeTime',
                columnWidth: .5,
                format: 'H:i',
                blankText: '执行时间为必填字段，不能为空！',
                beforeLabelTextTpl: ['<span style="color:red;">*</span>']
            }, {
                xtype: 'weekdaysfield',
                fieldLabel: '周几执行',
                columnWidth: 1,
                name: 'executeDays',
                allowBlank: false,
                blankText: '周几执行为必填字段，不能为空！',
                beforeLabelTextTpl: ['<span style="color:red;">*</span>']
            }]
        }, {
            itemId: 'month',
            items: [{
                fieldLabel: '执行时间',
                xtype: 'timefield',
                name: 'executeTime',
                columnWidth: .51,
                format: 'H:i',
                allowBlank: false,
                blankText: '执行时间为必填字段，不能为空！',
                beforeLabelTextTpl: ['<span style="color:red;">*</span>']
            }, {
                xtype: 'monthdaysfield',
                fieldLabel: '执行日期',
                columnWidth: 1,
                name: 'executeDays',
                allowBlank: false,
                blankText: '执行日期为必填字段，不能为空！',
                beforeLabelTextTpl: ['<span style="color:red;">*</span>']
            }]
        }, {
            itemId: 'periodic',
            items: [{
                fieldLabel: '执行间隔（秒）',
                xtype: 'numberfield',
                decimalPrecision: 0,
                name: 'intervalSeconds',
                columnWidth: .51,
                allowBlank: false,
                blankText: '执行间隔为必填字段，不能为空！',
                beforeLabelTextTpl: ['<span style="color:red;">*</span>']
            }, {
                fieldLabel: '执行次数',
                xtype: 'numberfield',
                decimalPrecision: 0,
                name: 'repeatTotalCount',
                columnWidth: .51,
                allowBlank: false,
                blankText: '执行次数为必填字段，不能为空！',
                beforeLabelTextTpl: ['<span style="color:red;">*</span>']
            }]
        }, {
            itemId: 'custom',
            items: [{
                fieldLabel: '计划表达式',
                xtype: 'textfield',
                name: 'cronExpression',
                columnWidth: .51,
                allowBlank: false,
                blankText: '计划表达式为必填字段，不能为空！',
                beforeLabelTextTpl: ['<span style="color:red;">*</span>'],
                emptyText: '*/5 * * * * ?'
            }, {
                xtype: 'container',
                columnWidth: 1,
                html: '<div><p>Provides a parser and evaluator for unix-like cron expressions.</p><table style="font-size: 11px"><tbody><tr><td><b>Example</b></td><td><b>Description</b></td></tr><tr><td><code>0 0 12 * * ?</code></td><td>Fire at 12pm (noon) every day</td></tr><tr><td><code>0 15 10 ? * *</code></td><td>Fire at 10:15am every day</td></tr><tr><td><code>0 15 10 * * ?</code></td><td>Fire at 10:15am every day</td></tr><tr><td><code>0 15 10 * * ? *</code></td><td>Fire at 10:15am every day</td></tr><tr><td><code>0 15 10 * * ? 2005</code></td><td>Fire at 10:15am every day during the year 2005</td></tr><tr><td><code>0 * 14 * * ?</code></td><td>Fire every minute starting at 2pm and ending at 2:59pm, every day</td></tr><tr><td><code>0 0/5 14 * * ?</code></td><td>Fire every 5 minutes starting at 2pm and ending at 2:55pm, every day</td></tr><tr valign="top"><td><code>0 0/5 14,18 * * ?</code></td><td>Fire every 5 minutes starting at 2pm and ending at 2:55pm, AND fire every 5 minutes starting at 6pm and ending at 6:55pm, every day</td></tr><tr><td><code>0 0-5 14 * * ?</code></td><td>Fire every minute starting at 2pm and ending at 2:05pm, every day</td></tr><tr><td><code>0 10,44 14 ? 3 WED</code></td><td>Fire at 2:10pm and at 2:44pm every Wednesday in the month of March.</td></tr><tr><td><code>0 15 10 ? * MON-FRI</code></td><td>Fire at 10:15am every Monday, Tuesday, Wednesday, Thursday and Friday</td></tr><tr><td><code>0 15 10 15 * ?</code></td><td>Fire at 10:15am on the 15th day of every month</td></tr><tr><td><code>0 15 10 L * ?</code></td><td>Fire at 10:15am on the last day of every month</td></tr><tr><td><code>0 15 10 ? * 6L</code></td><td>Fire at 10:15am on the last Friday of every month</td></tr><tr valign="top"><td nowrap=""><code>0 15 10 ? * 6L 2002-2005 &nbsp; &nbsp;</code></td><td>Fire at 10:15am on every last friday of every month during the years 2002, 2003, 2004 and 2005</td></tr><tr><td><code>0 15 10 ? * 6#3</code></td><td>Fire at 10:15am on the third Friday of every month</td></tr></tbody></table></div>'
            }]
        }]
    }]
})
;